﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GMUtilitiesDotNet.ViewModels;
using ThouShaltNotBrickLevelEditor.Code.Managers;
using GMUtilitiesDotNet.Commands;
using System.Windows.Input;
using GMUtilitiesDotNet.Services.ServiceInterfaces;

namespace ThouShaltNotBrickLevelEditor.ViewModels
{
    public sealed class MainMenuViewModel : ViewModelBase
    {
        #region Properties
        private CommandsManager mCommandsManager;
        private LevelManager mLevelManager;
        #endregion

        #region Commands
        #region LoadDataCommand
        private RelayCommand mLoadDataCommand;
        public ICommand LoadDataCommand
        {
            get
            {
                if (mLoadDataCommand == null)
                {
                    mLoadDataCommand = new RelayCommand(
                        param => OnLoadDataCommand(),
                        param => true);
                }
                return mLoadDataCommand;
            }
        }

        private void OnLoadDataCommand()
        {
            ISelectFolderService selectFolderService = ViewModelBase.ServiceProvider.Resolve<ISelectFolderService>();
            selectFolderService.Description = "Select folder with editor data:";
            bool dialogResult = selectFolderService.ShowDialog(null);
            if (dialogResult)
            {
                string dataFolderPath = selectFolderService.SelectedPath;
                mCommandsManager.LoadData(dataFolderPath);
            }
        }
        #endregion

        #region LoadLevelCommand
        private RelayCommand mLoadLevelCommand;
        public ICommand LoadLevelCommand
        {
            get
            {
                if (mLoadLevelCommand == null)
                {
                    mLoadLevelCommand = new RelayCommand(
                        param => OnLoadLevelCommand(),
                        param => CanLoadLevel());
                }
                return mLoadLevelCommand;
            }
        }

        private void OnLoadLevelCommand()
        {
            if (!CanLoadLevel())
            {
                return;
            }

            CustomDialogResults messageBoxResult = CustomDialogResults.Yes;
            if (mLevelManager.IsPendingLevelChanges)
            {
                messageBoxResult = ViewModelBase.ServiceProvider.Resolve<IMessageBoxService>().ShowYesNo(
                    "There are unsaved level changes that will be lost. Are you sure you wish to continue?",
                    CustomDialogIcons.None);
            }

            if (messageBoxResult != CustomDialogResults.Yes)
            {
                return;
            }

            IOpenFileService openFileService = ViewModelBase.ServiceProvider.Resolve<IOpenFileService>();
            openFileService.Filter = "LEVEL files (*.level)|*.level;";
            bool? dialogResult = ViewModelBase.ServiceProvider.Resolve<IOpenFileService>().ShowDialog(null);
            if (dialogResult.HasValue && dialogResult.Value == true)
            {
                string levelFilePath = ViewModelBase.ServiceProvider.Resolve<IOpenFileService>().FileName;
                mCommandsManager.LoadLevel(levelFilePath);
            }
        }

        private bool CanLoadLevel()
        {
            return mCommandsManager.CanLoadLevel();
        }
        #endregion

        #region SaveLevelCommand
        private RelayCommand mSaveLevelCommand;
        public ICommand SaveLevelCommand
        {
            get
            {
                if (mSaveLevelCommand == null)
                {
                    mSaveLevelCommand = new RelayCommand(
                        param => mCommandsManager.SaveLevel(),
                        param => mCommandsManager.CanSaveLevel());
                }
                return mSaveLevelCommand;
            }
        }
        #endregion

        #region SaveLevelAsCommand
        private RelayCommand mSaveLevelAsCommand;
        public ICommand SaveLevelAsCommand
        {
            get
            {
                if (mSaveLevelAsCommand == null)
                {
                    mSaveLevelAsCommand = new RelayCommand(
                        param => OnSaveLevelAsCommand(),
                        param => CanSaveLevelAs());
                }
                return mSaveLevelAsCommand;
            }
        }

        private void OnSaveLevelAsCommand()
        {
            if (!CanSaveLevelAs())
            {
                return;
            }

            ISaveFileService saveFileService = ViewModelBase.ServiceProvider.Resolve<ISaveFileService>();
            saveFileService.Filter = "LEVEL files (*.level)|*.level;";
            bool? dialogResult = ViewModelBase.ServiceProvider.Resolve<ISaveFileService>().ShowDialog(null);
            if (dialogResult.HasValue && dialogResult.Value == true)
            {
                string levelFilePath = ViewModelBase.ServiceProvider.Resolve<ISaveFileService>().FileName;
                mCommandsManager.SaveLevelAs(levelFilePath);
            }
        }

        private bool CanSaveLevelAs()
        {
            return mCommandsManager.CanSaveLevelAs();
        }
        #endregion

        #region ExitCommand
        private RelayCommand mExitCommand;
        public ICommand ExitCommand
        {
            get
            {
                if (mExitCommand == null)
                {
                    mExitCommand = new RelayCommand(
                        param => mCommandsManager.CloseMainWindow(),
                        param => true);
                }
                return mExitCommand;
            }
        }
        #endregion
        #endregion

        #region Constructors
        public MainMenuViewModel(LevelEditorManager pLevelEditorManager)
        {
            mCommandsManager = pLevelEditorManager.CommandsManager;
            mLevelManager = pLevelEditorManager.LevelManager;
        }
        #endregion
    }
}